package com.leyou.user.service.controller;

import com.leyou.common.bean.PageResult;
import com.leyou.common.bean.Result;
import com.leyou.user.inter.dto.PermissionPageDTO;
import com.leyou.user.inter.dto.RoleAuthorizeDTO;
import com.leyou.user.inter.pojo.Permission;
import com.leyou.user.inter.pojo.Role;
import com.leyou.user.inter.vo.AuthorizeTreeVO;
import com.leyou.user.inter.vo.MenuVO;
import com.leyou.user.service.service.AuthorizeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zqq
 * @ClassName AuthorizeController
 * @Description: 有关权限的类
 * @date 2020/3/24-17:47
 */
@RestController
@Api(value = "AuthorizeController",description = "权限相关接口")
@RequestMapping("author")
public class AuthorizeController {

    @Autowired
    private AuthorizeService authorizeService;

    @GetMapping("query/role/{uid}")
    @ApiOperation(value = "根据用户id查询角色", notes = "根据用户id查询角色")
    public Result<List<Role>> queryRoleByUid(
            @ApiParam(name = "uid",value = "用户id",required = true) @PathVariable(value = "uid") Long uid) {
        return this.authorizeService.queryRoleByUserId(uid);
    }

    @GetMapping("query/permission/{rid}")
    @ApiOperation(value = "根据角色id查询对应的权限", notes = "根据角色id查询对应的权限")
    public Result<List<Long>> queryPermissionByRid(
            @ApiParam(name = "rid",value = "角色id",required = true) @PathVariable(value = "rid") Long rid) {
        return this.authorizeService.queryPermissionByRid(rid);
    }

    @GetMapping("role/all")
    @ApiOperation(value = "查询所有的角色", notes = "查询所有的角色")
    public Result<List<Role>> queryRoleByAll() {
        return this.authorizeService.queryRoleByAll();
    }

    @PostMapping("role")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    public Result saveRole(@RequestBody Role role) {
        return this.authorizeService.updateRole(role);
    }

    @PutMapping("role")
    @ApiOperation(value = "修改角色", notes = "修改角色")
    public Result updateRole(@RequestBody Role role) {
        return this.authorizeService.updateRole(role);
    }

    @PutMapping("allocation")
    @ApiOperation(value = "权限分配", notes = "权限分配")
    public Result updateRoleAuthorize(@RequestBody RoleAuthorizeDTO dto) {
        return this.authorizeService.updateRoleAuthorize(dto);
    }

    @GetMapping("permission/all")
    @ApiOperation(value = "查询所有权限，并以树状结构返回", notes = "查询所有权限，并以树状结构返回")
    public Result<List<AuthorizeTreeVO>> queryPermissionByAll() {
        return this.authorizeService.queryPermissionByTree();
    }

    @DeleteMapping("role")
    @ApiOperation(value = "角色删除与恢复", notes = "角色删除与恢复")
    public Result deleteRole(@RequestBody Role dto) {
        return this.authorizeService.deleteRole(dto);
    }

    @PostMapping("permission/page")
    @ApiOperation(value = "分页查询权限列表", notes = "分页查询权限列表")
    public Result<PageResult<Permission>> queryPermissionByPage(@RequestBody PermissionPageDTO dto) {
        return this.authorizeService.queryPermissionByPage(dto);
    }

    @PostMapping("permission")
    @ApiOperation(value = "新增权限", notes = "新增权限")
    public Result savePermission(@RequestBody Permission dto) {
        return this.authorizeService.updatePermission(dto);
    }

    @PutMapping("permission")
    @ApiOperation(value = "修改权限", notes = "修改权限")
    public Result updatePermission(@RequestBody Permission dto) {
        return this.authorizeService.updatePermission(dto);
    }

    @DeleteMapping("permission")
    @ApiOperation(value = "禁用或启用权限", notes = "禁用或启用权限")
    public Result deletePermission(@RequestBody Permission dto) {
        return this.authorizeService.enablePermission(dto);
    }

    @GetMapping("permission/list/{pid}")
    @ApiOperation(value = "根据parentId查询子权限", notes = "根据parentId查询子权限")
    public Result<List<Permission>> queryPermissionByPid(
            @ApiParam(name = "pid",value = "权限id",required = true) @PathVariable(value = "pid") Long pid) {
        return this.authorizeService.queryPermissionByPid(pid);
    }

    @GetMapping("permission/get/{id}")
    @ApiOperation(value = "根据主键查询权限", notes = "根据主键查询权限")
    public Result<Permission> queryPermissionById(
            @ApiParam(name = "id",value = "权限id",required = true) @PathVariable(value = "id") Long id) {
        return this.authorizeService.queryPermissionById(id);
    }

    @GetMapping("query/menu")
    @ApiOperation(value = "获取用户一二级菜单", notes = "获取用户一二级菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result<List<MenuVO>> queryMenuByUser() {
        return this.authorizeService.queryMenuByUser();
    }

    @GetMapping("query/other/auth")
    @ApiOperation(value = "获取二级菜单下的所有权限", notes = "获取二级菜单下的所有权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result<List<Permission>> queryOtherAuthorize(@ApiParam(name = "url",value = "路由",required = true) @RequestParam(value = "url",required = true) String url) {
        return this.authorizeService.queryOtherAuthorize(url);
    }


}
